# ========================================
# 学生请假管理系统 - 开发环境启动脚本
# ========================================
# 功能: 初始化数据库 -> 启动后端 -> 启动前端
# 使用: .\start-dev.ps1
# ========================================

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "学生请假管理系统 - 开发环境启动脚本" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# 获取脚本所在目录
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Push-Location $scriptDir

# ========================================
# 检查 Node.js 和 npm
# ========================================
Write-Host "📋 检查环境..." -ForegroundColor Yellow
$nodeVersion = node -v 2>$null
$npmVersion = npm -v 2>$null

if (-not $nodeVersion) {
  Write-Host "❌ 未检测到 Node.js，请先安装 Node.js" -ForegroundColor Red
  exit 1
}

Write-Host "✅ Node.js: $nodeVersion" -ForegroundColor Green
Write-Host "✅ npm: $npmVersion" -ForegroundColor Green
Write-Host ""

# ========================================
# 检查环境变量文件
# ========================================
Write-Host "🔐 检查环境配置..." -ForegroundColor Yellow

$frontendEnvExists = Test-Path ".\.env"
$backendEnvExists = Test-Path ".\server\.env"

if (-not $frontendEnvExists) {
  Write-Host "⚠️  前端 .env 文件不存在，创建默认配置..." -ForegroundColor Yellow
  Set-Content -Path ".\.env" -Value "VITE_API_BASE_URL=http://localhost:8080/api" -Encoding UTF8
  Write-Host "✅ 已创建 .env" -ForegroundColor Green
}

if (-not $backendEnvExists) {
  Write-Host "❌ 后端 server/.env 文件不存在！" -ForegroundColor Red
  Write-Host "请先创建 server/.env 文件并配置数据库信息:" -ForegroundColor Yellow
  Write-Host "   DB_HOST=<your_host>"
  Write-Host "   DB_PORT=<your_port>"
  Write-Host "   DB_USER=<your_username>"
  Write-Host "   DB_PASSWORD=<your_password>"
  Write-Host "   DB_NAME=<your_database>"
  Write-Host "   PORT=8080"
  Write-Host "   NODE_ENV=development"
  Write-Host "   JWT_SECRET=<your_jwt_secret>"
  Write-Host ""
  $continue = Read-Host "已配置? (y/n)"
  if ($continue -ne "y") {
    Write-Host "脚本已中止" -ForegroundColor Red
    exit 1
  }
}

Write-Host "✅ 环境配置检查完成" -ForegroundColor Green
Write-Host ""

# ========================================
# 步骤 1: 初始化数据库
# ========================================
Write-Host "📊 步骤 1: 初始化数据库" -ForegroundColor Yellow
Write-Host "----------------------------------------" -ForegroundColor Yellow

$initScript = ".\server\init-db.js"
if (Test-Path $initScript) {
  Write-Host "运行数据库初始化脚本..." -ForegroundColor Cyan
  Push-Location ".\server"
  node ../init-db.js
  $initResult = $LASTEXITCODE
  Pop-Location
  
  if ($initResult -eq 0) {
    Write-Host "✅ 数据库初始化成功" -ForegroundColor Green
  } else {
    Write-Host "⚠️  数据库初始化可能失败，但继续启动服务..." -ForegroundColor Yellow
  }
} else {
  Write-Host "⚠️  初始化脚本不存在，跳过数据库初始化" -ForegroundColor Yellow
}

Write-Host ""

# ========================================
# 步骤 2: 启动后端和前端
# ========================================
Write-Host "🚀 步骤 2: 启动开发服务" -ForegroundColor Yellow
Write-Host "----------------------------------------" -ForegroundColor Yellow
Write-Host ""

# 创建两个新的 PowerShell 窗口
Write-Host "启动后端服务 (localhost:8080)..." -ForegroundColor Cyan
Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$scriptDir\server'; npm run dev" -WindowStyle Normal

Start-Sleep -Seconds 3

Write-Host "启动前端开发服务 (localhost:5173)..." -ForegroundColor Cyan
Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$scriptDir'; npm run dev" -WindowStyle Normal

Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "✅ 服务已启动！" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host "📌 访问地址:" -ForegroundColor Yellow
Write-Host "   前端: http://localhost:5173" -ForegroundColor Cyan
Write-Host "   后端: http://localhost:8080" -ForegroundColor Cyan
Write-Host "   API:  http://localhost:8080/api" -ForegroundColor Cyan
Write-Host ""
Write-Host "📋 测试账号:" -ForegroundColor Yellow
Write-Host "   学生: stu1 / 123456" -ForegroundColor Cyan
Write-Host "   老师: tea1 / 123456" -ForegroundColor Cyan
Write-Host "   管理员: admin / 123456" -ForegroundColor Cyan
Write-Host ""
Write-Host "💡 提示: 关闭两个新打开的 PowerShell 窗口即可停止服务" -ForegroundColor Yellow
Write-Host ""

Pop-Location

